#include "base_controller.h"

namespace Controller
{

    BaseController::BaseController(std::shared_ptr<httplib::Server> server, std::shared_ptr<Tshark::TsharkManager> tsharkManager)
        : m_tsharkManager(tsharkManager), m_server(server)
    {
    }

    int BaseController::getIntParam(const httplib::Request &req, std::string paramName, int defaultValue)
    {
        int value = defaultValue;
        for (auto it = req.params.begin(); it != req.params.end(); ++it)
        {
            if (it->first == paramName)
            {
                value = std::atoi(it->second.c_str());
            }
        }
        return value;
    }

    std::string BaseController::getStringParam(const httplib::Request &req, const std::string &paramName, const std::string &defaultValue)
    {
        std::string value = defaultValue;
        for (auto it = req.params.begin(); it != req.params.end(); ++it)
        {
            if (it->first == paramName)
            {
                value = it->second.c_str();
            }
        }
        return value;
    }

    void BaseController::sendResponJson(httplib::Response &resp, Json::Value &data)
    {
        Json::Value resJson;
        resJson["code"] = ERROR_SUCCESS;
        resJson["msg"] = Tshark::TsharkError::getErrorMsg(ERROR_SUCCESS);
        resJson["data"] = data;
        Json::StreamWriterBuilder writerBuidler;
        std::string resStr = Json::writeString(writerBuidler, resJson);
        resp.set_content(resStr, "application/json");
    }

    void BaseController::sendSuccessResponse(httplib::Response &resp)
    {
        Json::Value respJson;
        respJson["code"] = ERROR_SUCCESS;
        respJson["msg"] = Tshark::TsharkError::getErrorMsg(ERROR_SUCCESS);
        Json::StreamWriterBuilder writer;
        std::string respStr = Json::writeString(writer, respJson);
        resp.set_content(respStr, "application/json");
    }

    void BaseController::sendErrorResponse(httplib::Response &resp, int errorCode)
    {
        Json::Value respJson;
        respJson["code"] = errorCode;
        respJson["msg"] = Tshark::TsharkError::getErrorMsg(errorCode);
        Json::StreamWriterBuilder writer;
        std::string respStr = Json::writeString(writer, respJson);
        resp.set_content(respStr, "application/json");
    }

    bool BaseController::parseQueryCondition(const httplib::Request &req, Tshark::QueryConditon &condition)
    {
        Json::Value doc;
        Json::CharReaderBuilder readerBuidler;
        std::unique_ptr<Json::CharReader> reader(readerBuidler.newCharReader());
        std::string reqDoc = req.body.c_str(), errMsg;
        // 提取参数
        if (!reader->parse(reqDoc.c_str(), reqDoc.c_str() + reqDoc.size(), &doc, &errMsg))
        {
            LOG_F(ERROR, "failed to parse json: %s", errMsg.c_str());
            return false;
        }
        if (doc.isMember("ip") && doc["ip"].isString())
        {
            condition.ip = doc["ip"].asString();
        }
        if (doc.isMember("port") && doc["port"].isInt())
        {
            condition.port = doc["port"].asInt();
        }
        if (doc.isMember("location") && doc["location"].isString())
        {
            condition.location = doc["location"].asString();
        }
        if (doc.isMember("mac") && doc["mac"].isString())
        {
            condition.mac = doc["mac"].asString();
        }
        if (doc.isMember("proto") && doc["proto"].isString())
        {
            condition.proto = doc["proto"].asString();
        }
        return true;
    }
}